package com.metaera.app.data;


import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.metaera.app.MetaEraApplication;
import com.metaera.app.data.model.ActivityListResponse;
import com.metaera.app.data.model.AppConfigResponse;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.model.BasicResponse;
import com.metaera.app.data.model.CollectionListResponse;
import com.metaera.app.data.model.FeedbackResponse;
import com.metaera.app.data.model.FollowAuthorResponse;
import com.metaera.app.data.model.HistoryListResponse;
import com.metaera.app.data.model.HomeNewsTypeResponse;
import com.metaera.app.data.model.HomeTopNewsResponse;
import com.metaera.app.data.model.HotColumnResponse;
import com.metaera.app.data.model.HotSearchResponse;
import com.metaera.app.data.model.InformationDetailsResponse;
import com.metaera.app.data.model.InvestFinanceResponse;
import com.metaera.app.data.model.MorningPostResponse;
import com.metaera.app.data.model.NoticeListResponse;
import com.metaera.app.data.model.PhoneCodeResponse;
import com.metaera.app.data.model.ProfileResponse;
import com.metaera.app.data.model.SearchResponse;
import com.metaera.app.data.model.SearchResultResponse;
import com.metaera.app.data.model.SpecialDetailsResponse;
import com.metaera.app.data.model.SpecialResponse;
import com.metaera.app.data.model.TodayEventResponse;
import com.metaera.app.data.model.UploadImageResponse;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.data.model.UserNoticeResponse;
import com.metaera.app.data.model.VersionUpInfoResponse;

import java.io.File;
import java.util.List;
import java.util.Locale;

import io.reactivex.Observable;

/**
 * 缓存
 */
public class LocalCacheDataSource implements IDataSource {

    public final static String
            homeTopNews           = "homeTopNews",
            getPhoneCode           = "getPhoneCode",
            hotSearch             = "hotSearch",
            getAppConfig          = "getAppConfig",
            categoryLists_key     = "categoryLists(%1d)",
            relatedRecommend      = "relatedRecommend(%1s)",
            getSpecialDetails     = "getSpecialDetails(%1s)",
            getInformationDetails = "getInformationDetails(%1s)",
            getInvestDetails      = "getInvestDetails(%1d,%2d)",
            calendarList          = "calendarList(%1s,%1d)",
            appointDateCalendar   = "appointDateCalendar(%1s,%1d)",
            getSpecial            = "getSpecial(%1d,%2d)",
            getFollowLists        = "getFollowLists(%1d,%2d)",
            getFollowList        = "getFollowList(%1d,%2d)",
            getEventDetails       = "getEventDetails(%1d,%2d)",
            getRecommendLists     = "getRecommendLists(%1d,%2d)",
            search                = "search(%s,%1d,%2d)",
            getActivityList       = "getActivityList(%1d,%2d,%3d)",
            getPopularColumn       = "getPopularColumn(%1d,%2d,%3d)",
            getArticleList        = "getArticleList(%1d,%2d,%3d,%4d)",
            getNoticeList         = "getNoticeList(%1d,%2d,%3d)";

    private final SharedPreferences mPreferences;

    public LocalCacheDataSource() {
        mPreferences = MetaEraApplication.sInstance.getSharedPreferences("data_local_cache", Context.MODE_PRIVATE);
    }


    @Override
    public Observable<BasicResponse<UploadImageResponse>> upload(File file) {
        return null;
    }

    @Override
    public Observable<BasicResponse<List<HotSearchResponse>>> hotSearch() {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(hotSearch, null);

            if (data != null) {
                BasicResponse<List<HotSearchResponse>> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<List<HotSearchResponse>>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<HomeTopNewsResponse>> homeTopNews() {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(homeTopNews, null);

            if (data != null) {
                BasicResponse<HomeTopNewsResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<HomeTopNewsResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<MorningPostResponse>> getNewsDetails(int page, int size) {
        return null;
    }

    @Override
    public Observable<BasicListResponse<InvestFinanceResponse>> getInvestDetails(int page, int size) {

        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getInvestDetails, page, size), null);

            if (data != null) {
                BasicListResponse<InvestFinanceResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<InvestFinanceResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<TodayEventResponse>> getEventDetails(int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getEventDetails, page, size), null);

            if (data != null) {
                BasicListResponse<TodayEventResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<TodayEventResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }


            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<List<HomeNewsTypeResponse>>> categoryLists(int type) {

        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), categoryLists_key, type), null);

            if (data != null) {
                BasicResponse<List<HomeNewsTypeResponse>> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<List<HomeNewsTypeResponse>>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }


            emitter.onComplete();
        });

    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getArticleList(int category_id, int type, int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getArticleList,
                            category_id, type, page, size), null);

            if (data != null) {
                BasicListResponse<ArticleListResponse> response = new Gson().fromJson(data,
                        new TypeToken<BasicListResponse<ArticleListResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }


            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<UserDetailsResponse>> getUserInfo() {
        return null;
    }

    @Override
    public Observable<BasicResponse<ProfileResponse>> toUserInfo(int id) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> editUserInfo(
            String nickname, String avatar_img, String sex, String position, String desc,
            String weibo, String official_account, String twitter, String email,
            String is_show_phone, String phone, String knowledge_planet,
            String is_show_wechat, String wechat) {
        return null;
    }


    @Override
    public Observable<BasicResponse<UserDetailsResponse>> login(
            int type, String code, String mobile, String password, String phone_code) {
        return null;
    }

    @Override
    public Observable<BasicResponse<List<PhoneCodeResponse>>> getPhoneCode() {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getPhoneCode), null);

            if (data != null) {
                BasicResponse<List<PhoneCodeResponse>> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<List<PhoneCodeResponse>>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<Object>> sendSms(String code, String mobile) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> setPassword(String code, String mobile, String phone_code, String password1, String password2) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> forgotPassword(String code, String mobile, String phone_code, String password1, String password2) {
        return null;
    }

    @Override
    public Observable<BasicListResponse<HistoryListResponse>> getHistoryList(int page, int size) {
        return null;
    }

    @Override
    public Observable<BasicListResponse<CollectionListResponse>> myCollection(int page, int size) {
        return null;
    }

    @Override
    public Observable<BasicListResponse<NoticeListResponse>> getNoticeList(int status, int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getNoticeList, status, page, size), null);

            if (data != null) {
                BasicListResponse<NoticeListResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<NoticeListResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<List<FeedbackResponse>>> getFeedbackLog() {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> submitFeedback(int type, String content, String name, String contact) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> cleanHistory() {
        return null;
    }

    @Override
    public Observable<BasicResponse<List<ArticleListResponse>>> relatedRecommend(String id) {
        return Observable.create(emitter -> {
            // 此处不按接口存储
            String data = mPreferences.getString(String.format(Locale.getDefault(), relatedRecommend, 0), null);

            if (data != null) {
                BasicResponse<List<ArticleListResponse>> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<List<ArticleListResponse>>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<InformationDetailsResponse>> getInformationDetails(String id) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), getInformationDetails, id), null);

            if (data != null) {
                BasicResponse<InformationDetailsResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<InformationDetailsResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<HotColumnResponse>> getPopularColumn(int type, int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), getPopularColumn, type,page, size), null);

            if (data != null) {
                BasicListResponse<HotColumnResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<HotColumnResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<SpecialResponse>> getSpecial(int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), getSpecial, page, size), null);

            if (data != null) {
                BasicResponse<SpecialResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<SpecialResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> appointDateCalendar(String date, int categoryID) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), appointDateCalendar, date, categoryID), null);

            if (data != null) {
                BasicListResponse<ArticleListResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<ArticleListResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<List<String>>> calendarList(String date, int categoryID) {

        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), calendarList, date, categoryID), null);

            if (data != null) {
                BasicResponse<List<String>> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<List<String>>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getUserInformation(String id, int page, int size) {
        return null;
    }

    @Override
    public Observable<BasicListResponse<ActivityListResponse>> getActivityList(int type, int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), getActivityList, type, page, size), null);

            if (data != null) {
                BasicListResponse<ActivityListResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<ActivityListResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getFollowLists(int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), getFollowLists, page, size), null);

            if (data != null) {
                BasicListResponse<ArticleListResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<ArticleListResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<ActivityListResponse>> getActivityDetails(int id) {
        return null;
    }

    @Override
    public Observable<BasicResponse<AppConfigResponse>> getAppConfig() {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(getAppConfig, null);

            if (data != null) {
                BasicResponse<AppConfigResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<AppConfigResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<SearchResultResponse>> searchMore(String key, int orderType, int timeType, int type, int page, int size) {
        return null;
    }

    @Override
    public Observable<BasicResponse<SearchResponse>> search(String key, int orderType, int timeType) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(String.format(Locale.getDefault(), search, key, orderType, timeType), null);

            if (data != null) {
                BasicResponse<SearchResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<SearchResponse>>() {
                        }.getType());

                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getRecommendLists(int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getRecommendLists, page, size), null);

            if (data != null) {
                BasicListResponse<ArticleListResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<ArticleListResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getLabelDetails(int id, int page, int size) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> followAuthor(int author_id, int is_status) {
        return null;
    }

    @Override
    public Observable<BasicResponse<VersionUpInfoResponse>> versionUpInfo() {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> likeInformation(int id, int is_status, int type) {
        return null;
    }

    @Override
    public Observable<BasicResponse<SpecialDetailsResponse>> getSpecialDetails(int id) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getSpecialDetails, id), null);

            if (data != null) {
                BasicResponse<SpecialDetailsResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicResponse<SpecialDetailsResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<UserDetailsResponse>> weChatLogin(String code) {
        return null;
    }

    @Override
    public Observable<BasicResponse<UserDetailsResponse>> bindMobile(
            String code, String mobile, String phone_code,String token) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> collectionInformation(int id) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> cancelCollectionInformation(int id) {
        return null;
    }

    @Override
    public Observable<BasicListResponse<FollowAuthorResponse>> getFollowList(int page, int size) {
        return Observable.create(emitter -> {
            String data = mPreferences.getString(
                    String.format(Locale.getDefault(), getFollowList, page, size), null);

            if (data != null) {
                BasicListResponse<FollowAuthorResponse> response =
                        new Gson().fromJson(data, new TypeToken<BasicListResponse<FollowAuthorResponse>>() {
                        }.getType());
                emitter.onNext(response);

            } else {

                emitter.onNext(new BasicListResponse<>());
            }

            emitter.onComplete();
        });
    }

    @Override
    public Observable<BasicResponse<Object>> updateAuthorNotice(int id, int is_notice) {
        return null;
    }

    @Override
    public Observable<BasicResponse<UserNoticeResponse>> getUserNotice() {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> updateUserNotice(
            int id, String is_new_notice, String is_disturb, String disturb_start_time, String disturb_end_time,
            String is_article, String is_news_flash, String is_notice, String is_author_update) {
        return null;
    }

    @Override
    public Observable<BasicResponse<Object>> logOff() {
        return null;
    }


}
